using System.Xml;
using NUnit.Framework;
using System.Windows.Forms;
using Send2Sharepoint.Forms;

namespace IndexDocumentsTests
{
    [TestFixture]
    public class BuildLabelField
    {
        [Test]
        public void WillCreateALabel()
        {
            var doc = CreateGenericXmlDocument();
            AddAttributeTextBoxType(doc);
            AddNameAndDisplayNameAttributes(doc);
            var labelField = BuildLabelAndField(doc);
            Assert.That(labelField.Label.Text, Is.EqualTo("Display Name"));
        }

        [Test]
        public void WillCreateATextBoxAndMovesALine()
        {
            var doc = CreateGenericXmlDocument();
            AddAttributeTextBoxType(doc);
            AddNameAndDisplayNameAttributes(doc);
            var labelField = BuildLabelAndField(doc);
            Assert.That(labelField.Field,Is.InstanceOf(typeof(TextBox)));
            Assert.That(labelField.Field.Height, Is.EqualTo(20));
        }

        [Test]
        public void WillCreateATextAreaAndMovesThreeLines()
        {
            var doc = CreateGenericXmlDocument();
            AddAttributeTextAreaType(doc);
            AddNameAndDisplayNameAttributes(doc);
            var labelField = BuildLabelAndField(doc);
            Assert.That(labelField.Field, Is.InstanceOf(typeof(TextBox)));
            Assert.That(((TextBox)labelField.Field).Multiline,Is.EqualTo(true));
            Assert.That(labelField.Field.Height, Is.EqualTo(66));
        }

        [Test]
        public void WillCreateAComboBox()
        {
            var doc = CreateChoiceXmlDocument();
            AddAttributeDropdownType(doc);
            AddNameAndDisplayNameAttributes(doc);
            var labelField = BuildLabelAndField(doc);
            Assert.That(labelField.Field, Is.InstanceOf(typeof(ComboBox)));
            Assert.That(labelField.Field.Height, Is.EqualTo(21));
        }

        [Test]
        public void WillCreateARadioButtonGroup()
        {
            var doc = CreateChoiceXmlDocument();
            AddAttributeRadiobuttonType(doc);
            AddNameAndDisplayNameAttributes(doc);
            var labelField = BuildLabelAndField(doc);
            Assert.That(labelField.Field, Is.InstanceOf(typeof(GroupBox)));
            Assert.That(labelField.Field.Controls[0],Is.TypeOf(typeof(RadioButton)));
        }

        [Test]
        public void WillCreateACheckBoxGroup()
        {
            var doc = CreateChoiceXmlDocument();
            AddAttributeCheckBoxType(doc);
            AddNameAndDisplayNameAttributes(doc);
            var labelField = BuildLabelAndField(doc);
            Assert.That(labelField.Field, Is.InstanceOf(typeof(GroupBox)));
            Assert.That(labelField.Field.Controls[0], Is.TypeOf(typeof(CheckBox)));
        }

        [Test]
        public void WillCreateANumberAsTextBox()
        {
            var doc = CreateChoiceXmlDocument();
            AddAttributeNumberType(doc);
            AddNameAndDisplayNameAttributes(doc);
            var labelField = BuildLabelAndField(doc);
            Assert.That(labelField.Field, Is.InstanceOf(typeof(TextBox)));
        }

        [Test]
        public void WillAppendPercentageToLabelWhenFieldIsNumberAndPercentage()
        {
            var doc = CreateChoiceXmlDocument();
            AddAttributeNumberType(doc);
            AddNameAndDisplayNameAttributes(doc);
            AddPercentageAttribute(doc);
            var labelField = BuildLabelAndField(doc);
            Assert.That(labelField.Label.Text, Is.EqualTo("Display Name%"));
        }

        [Test]
        public void WillAttachARequiredHandlerWhenFieldIsRequired()
        {
            var doc = CreateGenericXmlDocument();
            AddAttributeTextBoxType(doc);
            AddRequiredAttribute(doc);
            AddNameAndDisplayNameAttributes(doc);
            var labelField = BuildLabelAndField(doc);
            var textField = (TextBox) labelField.Field;
            //TBD
        }

        private static void AddAttributeRadiobuttonType(XmlDocument doc)
        {
            var xmlAttribute = doc.CreateAttribute("Type");
            xmlAttribute.Value = "Choice";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
            xmlAttribute = doc.CreateAttribute("Format");
            xmlAttribute.Value = "RadioButtons";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
        }
        private static void AddAttributeCheckBoxType(XmlDocument doc)
        {
            var xmlAttribute = doc.CreateAttribute("Type");
            xmlAttribute.Value = "MultiChoice";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
        }

        private static void AddAttributeNumberType(XmlDocument doc)
        {
            var xmlAttribute = doc.CreateAttribute("Type");
            xmlAttribute.Value = "Number";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
        }

        private static void AddAttributeDropdownType(XmlDocument doc)
        {
            var xmlAttribute = doc.CreateAttribute("Type");
            xmlAttribute.Value = "Choice";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
            xmlAttribute = doc.CreateAttribute("Format");
            xmlAttribute.Value = "Dropdown";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
        }

        private static InputControl BuildLabelAndField(XmlDocument doc)
        {
            var inputControlFactoryImplementation = new InputControlFactoryImplementation();
            return inputControlFactoryImplementation.MakeInputControl(doc.DocumentElement);
        }

        private static XmlDocument CreateGenericXmlDocument()
        {
            var doc = new XmlDocument();
            doc.LoadXml("<node></node>");
            return doc;
        }

        private static XmlDocument CreateChoiceXmlDocument()
        {
            var doc = new XmlDocument();
            doc.LoadXml("<node><Default>Default</Default><CHOICES><CHOICE>Default</CHOICE><CHOICE>A</CHOICE><CHOICE>B</CHOICE><CHOICE>C</CHOICE></CHOICES></node>");
            return doc;
        }

        private static void AddAttributeTextAreaType(XmlDocument doc)
        {
            var xmlAttribute = doc.CreateAttribute("Type");
            xmlAttribute.Value = "Note";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
        }

        private static void AddAttributeTextBoxType(XmlDocument doc)
        {
            var xmlAttribute = doc.CreateAttribute("Type");
            xmlAttribute.Value = "Text";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
        }
        private static void AddRequiredAttribute(XmlDocument doc)
        {
            XmlAttribute xmlAttribute = doc.CreateAttribute("Required");
            xmlAttribute.Value = "TRUE";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
        }
        private static void AddPercentageAttribute(XmlDocument doc)
        {
            XmlAttribute xmlAttribute = doc.CreateAttribute("Percentage");
            xmlAttribute.Value = "TRUE";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
        }

        private static void AddNameAndDisplayNameAttributes(XmlDocument doc)
        {
            XmlAttribute xmlAttribute = doc.CreateAttribute("Name");
            xmlAttribute.Value = "Name";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
            xmlAttribute = doc.CreateAttribute("DisplayName");
            xmlAttribute.Value = "Display Name";
            doc.DocumentElement.Attributes.Append(xmlAttribute);
        }
    }
}